package cn.wolfcode.service.Impl;

import cn.wolfcode.domain.Score;
import cn.wolfcode.mapper.ScoreMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class ScoreServiceImpl implements IScoreService {
    @Autowired
    private ScoreMapper scoreMapper;
    @Override
    public void insert(Score score) {
        scoreMapper.insert(score);
    }

    @Override
    public void delete(Long id) {
        scoreMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Score score) {
        scoreMapper.updateByPrimaryKey(score);

    }

    @Override
    public Score selectOne(Long id) {
        return scoreMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Score> selectList() {
        return scoreMapper.selectAll();
    }

    @Override
    public PageResult<Score> query(QueryObject qo) {
        int totalCount = scoreMapper.queryForCount(qo);
        if (totalCount==0){
            return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),totalCount, Collections.emptyList());
        }
        List<Score> scores = scoreMapper.queryForList(qo);
        return new PageResult<>(qo.getCurrentPage(),qo.getPageSize(),totalCount,scores);
    }
}
